package com.controller;

import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.Collections;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.http.RequestEntity;
import org.springframework.http.ResponseEntity;
import org.springframework.util.MultiValueMap;
import org.springframework.util.StreamUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

//@RestController
public class ProxyController {

    @RequestMapping(value = "/**",consumes = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<byte[]> proxy(HttpServletRequest request, HttpServletResponse response, HttpMethod method)
        throws URISyntaxException, IOException {
        System.out.println("appicatoin/json请求");
        RestTemplate restTemplate = new RestTemplate();
        String redirectUrl = createRedictUrl(request, "http://127.0.0.1:8080", "http://127.0.0.1:80");
        System.out.println("route to -> " + redirectUrl);
        RequestEntity<byte[]> requestEntity = createRequestEntity(request, redirectUrl);
        ResponseEntity<byte[]> responseStr = restTemplate.exchange(requestEntity, byte[].class);
        return responseStr;
    }

    private RequestEntity<byte[]> createRequestEntity(HttpServletRequest request, String url)
        throws URISyntaxException, IOException {
        String method = request.getMethod();
       
        HttpMethod httpMethod = HttpMethod.resolve(method);
        MultiValueMap<String, String> headers = parseRequestHeader(request);
        byte[] body = parseRequestBody(request);
        System.out.println("body:"+new String(body)+","+request.getQueryString());
        return new RequestEntity<byte[]>(body, headers, httpMethod, new URI(url));
    }

    private String createRedictUrl(HttpServletRequest request, String routeUrl, String prefix) {
        String queryString = request.getQueryString();
        return routeUrl + request.getRequestURI().replace(prefix, "") + (queryString != null ? "?" + queryString : "");
    }

    private byte[] parseRequestBody(HttpServletRequest request) throws IOException {
        InputStream inputStream = request.getInputStream();
        return StreamUtils.copyToByteArray(inputStream);
    }

    private MultiValueMap<String, String> parseRequestHeader(HttpServletRequest request) {
        System.out.println("-------header--start---request@" + request.hashCode());
        HttpHeaders headers = new HttpHeaders();
        List<String> headerNames = Collections.list(request.getHeaderNames());
        for (String headerName : headerNames) {
            System.out.print(headerName + ": ");
            List<String> headerValues = Collections.list(request.getHeaders(headerName));
            for (String headerValue : headerValues) {
                System.out.print(headerValue + " ");
                headers.add(headerName, headerValue);
            }
            System.out.println();
        }
        System.out.println("-------header--end---request@" + request.hashCode());
        return headers;
    }
}
